package com.steellee.leeexcel;

import com.steellee.leeexcel.dto.MetaExcelReadDTO;
import com.steellee.leeexcel.msg.JSONMessage;
import com.steellee.leeexcel.service.PaExcelReadStart;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PaExcelReadForMapDemo extends Parent {
    private static final Logger logger = LoggerFactory.getLogger(PaExcelReadStart.class);
    @Autowired
    private PaExcelRead operate;

    /**
     * 测试数据获取-封装入参， 多个 sheet读取 返回 map
     * 参数: inportStream, class, 入参封装类集合
     *
     * @ throws Exception
     */
    @Test

    public void getDataReturnMapMoreSheetTureTest() throws Exception {
        List<MetaExcelReadDTO> param = new ArrayList<>();
        MetaExcelReadDTO dto1 = new MetaExcelReadDTO();
        dto1.setDataStartRowNo(1);
        dto1.setSheetNo(0);
        param.add(dto1);
        MetaExcelReadDTO dto2 = new MetaExcelReadDTO();
        dto2.setDataStartRowNo(1);
        dto2.setSheetNo(1);
        param.add(dto2);
        FileInputStream inputStream = getTureInputStream();
        JSONMessage result = operate.readForMap(inputStream, param);
        System.out.println("1返回码: " + result.responseCode + ", 返回信息: " + result.responseMsg);
    }

    public FileInputStream getTureInputStream() throws IOException {
        String excelPath = System.getProperty("file.separator") + "template" + System.getProperty("file.separator") + "excelAnalysisTrue.xlsx";
        FileInputStream ins = new FileInputStream((new ClassPathResource(excelPath)).getFile());
        return ins;
    }

    /**
     * 测试数据获取-封装入参， 多个 sheet读取 返回 map
     * 参数: inportstream, class, 入参封装类集合
     *
     * @ throws Exception
     */
    @Test

    public void testReadForMapSuccess() throws Exception {
        List<MetaExcelReadDTO> param = new ArrayList<>();
        MetaExcelReadDTO dto1 = new MetaExcelReadDTO();
        dto1.setDataStartRowNo(1);
        dto1.setSheetNo(0);
        dto1.setHeads(getHeads());
        param.add(dto1);
        MetaExcelReadDTO dto2 = new MetaExcelReadDTO();
        dto2.setDataStartRowNo(1);
        dto2.setSheetNo(1);
        dto2.setHeads(getHeads());
        param.add(dto2);
        FileInputStream inputStream = getTureInputStream();
        JSONMessage result = operate.readForMap(inputStream, param);
        System.out.println("1返回码: " + result.responseCode + ", 返回信息: " + result.responseMsg);
    }

    /**
     * 测试数据获取-封装入参，多个 sheet读取 返回 map
     * 参数: inportStream, class, 入参封装类集合
     *
     * @ throws Exception
     */
    @Test
    public void testReadForMapFailReturnWithOwnCheck() throws Exception {
        List<MetaExcelReadDTO> param = new ArrayList<>();
        MetaExcelReadDTO dto1 = new MetaExcelReadDTO();
        dto1.setDataStartRowNo(1);
        dto1.setSheetNo(0);
        dto1.setHeads(getHeads());
        dto1.setAbstractClazz(OwnMapCheck.class);
        param.add(dto1);
        MetaExcelReadDTO dto2 = new MetaExcelReadDTO();
        dto2.setDataStartRowNo(1);
        dto2.setSheetNo(1);
        dto2.setHeads(getHeads());
        dto2.setAbstractClazz(OwnMapCheck.class);
        param.add(dto2);
        JSONMessage msg = operate.readForMap(getFalseInputStream(), param);
        Map<String, Object> data = (Map<String, Object>) msg.result;

        JSONMessage result = operate.mapDataCheck(param, data, null, getObjTemplate());
        System.out.println("1返回码: " + result.responseCode + ", 返回信息: " + result.responseMsg);
    }

    public String[] getHeads() {
        return new String[]{"aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh"};
    }

    /**
     * 获取有问题的 excel文件
     *
     * @return fileInputStream
     * @ throws IOException
     */
    public FileInputStream getFalseInputStream() throws IOException {
        String excelPath = System.getProperty("file.separator") + "template" + System.getProperty("file.separator") + "excelAnalysisFalse.xlsx";
        FileInputStream ins = new FileInputStream((new ClassPathResource(excelPath)).getFile());
        return ins;
    }

    /**
     * 获取模板
     *
     * @return fileInputStream
     * @ throws IOException
     */
    public FileInputStream getObjTemplate() throws IOException {
        String excelPath = System.getProperty("file.separator") + "template" + System.getProperty("file.separator") + "objTemplate.xlsx";
        FileInputStream fileInputStream = new FileInputStream((new ClassPathResource(excelPath)).getFile());
        return fileInputStream;
    }
}